﻿import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

import com.sun.management.OperatingSystemMXBean;

@SuppressWarnings("restriction")
public class OSTest {

    public static void main(String[] args) {
        double[] n1 = new double[1000000];
        double[] n2 = new double[1000000];
        
        OperatingSystemMXBean osmb = (OperatingSystemMXBean) ManagementFactory
                .getOperatingSystemMXBean();
        System.out.println("물리메모리 총메모리 : " + bitToMega(osmb.getTotalPhysicalMemorySize()) + "MB");
        System.out.println("물리메모리 사용량 : " + bitToMega(osmb.getCommittedVirtualMemorySize()) + "MB");
        System.out.println("물리메모리 사용비율 : " +(osmb.getCommittedVirtualMemorySize())/(osmb.getTotalPhysicalMemorySize())*100 + "%");
        
        System.out.println("===================");
        System.out.println("가상메모리 총량 : " + bitToMega(osmb.getTotalSwapSpaceSize()) + "MB");
        System.out.println("가상메모리 사용량 : " +bitToMega(osmb.getFreeSwapSpaceSize()) + "MB");
        
        // System.out.println(osmb.getSystemLoadAverage()); //1.6이상
        System.out.println(osmb.getName() + " : " + osmb.getArch() + " : "
                + osmb.getVersion());

        System.out.println(osmb.getProcessCpuTime());

        System.out.println(osmb.getAvailableProcessors());
        
        
        
        OperatingSystemMXBean osbean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        RuntimeMXBean runbean = (RuntimeMXBean) ManagementFactory.getRuntimeMXBean();


        long bfprocesstime = osbean.getProcessCpuTime();
        long bfuptime = runbean.getUptime();
        long ncpus = osbean.getAvailableProcessors();


        for (int i = 0; i < 1000000; ++i) {
        ncpus = osbean.getAvailableProcessors();
        }


        long afprocesstime = osbean.getProcessCpuTime();
        long afuptime = runbean.getUptime();


        float cal = (afprocesstime - bfprocesstime) / ((afuptime - bfuptime) * 10000f);
        
        float javacpu = Math.min(99f, cal);
        
        System.out.println("javacpu : " + javacpu);
        
        Runtime rt  = Runtime.getRuntime();
        int pn = rt.availableProcessors();
        String str = Integer.toString(pn);
        System.out.println("Processor number: " + str );   

    }

    public static long bitToMega(long bit) {
        long mega = 0L;

        mega = bit / 1024 / 1024;

        return mega;
    }
}
